package com.loongtech.bi.manager.player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loongtech.bi.manager.game.LocationConstantManager;
import com.loongtech.bi.manager.game.UserLoginLocationManager;
import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;

/**
 * 玩家分析-版本分析manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class VersionAndHardwarePlayerManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserLoginLocationManager userLoginLocationManager;
	@Autowired
	private LocationConstantManager locationConstantManager;
	
	//城市人数分布
	public List<UserLoginLocationCityData> getUserLoginLocationCityData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		List<UserLoginLocationCityData> result = new ArrayList<UserLoginLocationCityData>();
		List<Object[]> userLoginLocationList = userLoginLocationManager.getUserNumberByAllKindGroupByCityId(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);		
		for(Object[] o : userLoginLocationList) {
			UserLoginLocationCityData UserLoginLocationCityData = new UserLoginLocationCityData();
			
			List<Object[]> list = locationConstantManager.getCityNameByCityId(o[0].toString());
			
			if(list != null && list.size() > 0) {
				UserLoginLocationCityData.setCity(list.get(0)[0].toString());
			}
			
			UserLoginLocationCityData.setUserNumber(Integer.parseInt(o[1].toString()));
			result.add(UserLoginLocationCityData);
		}
		
		Collections.sort(result, new UserLoginLocationCityData().comparatorNum);

		
		return result;
	}
	
	//省份人数分布
	public List<UserLoginLocationProvinceData> getUserLoginLocationProvinceData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		List<UserLoginLocationProvinceData> result = new ArrayList<UserLoginLocationProvinceData>();
		List<Object[]> userLoginLocationList = userLoginLocationManager.getUserNumberByAllKindGroupByProvinceId(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);		
		for(Object[] o : userLoginLocationList) {
			UserLoginLocationProvinceData UserLoginLocationProvinceData = new UserLoginLocationProvinceData();
			
			List<Object[]> list = locationConstantManager.getprovinceNameByProvinceId(o[0].toString());
			
			if(list != null && list.size() > 0) {
				UserLoginLocationProvinceData.setProvince(list.get(0)[0].toString());
			}
			
			UserLoginLocationProvinceData.setUserNumber(Integer.parseInt(o[1].toString()));
			result.add(UserLoginLocationProvinceData);
		}
		
		Collections.sort(result, new UserLoginLocationProvinceData().comparatorNum);
		
		return result;
	}
	
	//城市
	public class UserLoginLocationCityData {
		private String platformId;
		private String channelId;
		private String serverId;
		private String day; // 日期
		@ChartFlow(name="城市")
		private String city = "未知";
		@ChartSelect(name = "人数", key = 1)
		@ChartData1(name = "人数", key = 1)
		private int userNumber;
		
		// 根据日期排序的比较器
		public final Comparator<UserLoginLocationCityData> comparatorNum = new Comparator<UserLoginLocationCityData>() {
			public int compare(UserLoginLocationCityData s1, UserLoginLocationCityData s2) {
				return new Integer(s1.getUserNumber()).compareTo(new Integer(s2.getUserNumber()));
			}
		};
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId;
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public String getCity() {
			return city;
		}
		public void setCity(String city) {
			this.city = city;
		}
		public int getUserNumber() {
			return userNumber;
		}
		public void setUserNumber(int userNumber) {
			this.userNumber = userNumber;
		}
	}
	
	//省份
	public class UserLoginLocationProvinceData {
		private String platformId;
		private String channelId;
		private String serverId;
		private String day; // 日期
		@ChartFlow(name="省份")
		private String province = "未知";
		@ChartSelect(name = "人数", key = 1)
		@ChartData1(name = "人数", key = 1)
		private int userNumber;
		
		// 根据日期排序的比较器
		public final Comparator<UserLoginLocationProvinceData> comparatorNum = new Comparator<UserLoginLocationProvinceData>() {
			public int compare(UserLoginLocationProvinceData s1, UserLoginLocationProvinceData s2) {
				return new Integer(s1.getUserNumber()).compareTo(new Integer(s2.getUserNumber()));
			}
		};
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId;
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public String getProvince() {
			return province;
		}
		public void setProvince(String province) {
			this.province = province;
		}
		public int getUserNumber() {
			return userNumber;
		}
		public void setUserNumber(int userNumber) {
			this.userNumber = userNumber;
		}
	}
}
